package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.entity.TeacherLike;
import com.hy.mapper.TeacherLikeMapper;
import com.hy.service.TeacherLikeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class TeacherLikeServiceImpl extends ServiceImpl<TeacherLikeMapper, TeacherLike> implements TeacherLikeService {

    @Override
    @Transactional
    public boolean addLike(TeacherLike teacherLike) {
        teacherLike.setDeleteFlag(0);
        teacherLike.setCreateTime(new Date());
        teacherLike.setUpdateTime(new Date());
        return save(teacherLike);
    }

    @Override
    @Transactional
    public boolean cancelLike(Integer id) {
        LambdaUpdateWrapper<TeacherLike> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TeacherLike::getId, id)
                    .set(TeacherLike::getDeleteFlag, 1)
                    .set(TeacherLike::getUpdateTime, LocalDateTime.now());
        return update(updateWrapper);
    }

    @Override
    public boolean isLiked(Integer userId, Integer teacherId) {
        LambdaQueryWrapper<TeacherLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherLike::getUserId, userId)
               .eq(TeacherLike::getTeacherId, teacherId)
               .eq(TeacherLike::getDeleteFlag, 0);
        return count(wrapper) > 0;
    }

    @Override
    public Long getTeacherLikeCount(Long teacherId) {
        LambdaQueryWrapper<TeacherLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherLike::getTeacherId, teacherId)
               .eq(TeacherLike::getDeleteFlag, 0);
        return count(wrapper);
    }
} 